package cn.donting.jme.edit.attribute;

import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import javafx.scene.Node;
import javafx.scene.control.TextField;

import java.io.IOException;

public class StringAttribute extends Attribute<String> {
    private TextField textField = new TextField();
    public StringAttribute(String title) {
        super(title);
        textField.textProperty().addListener((observable, oldValue, newValue)
                -> setSuperValue(newValue));
    }

    public StringAttribute() {
        this("");
    }

    @Override
    public void setValue(String value) {
        textField.setText(value);
    }

    private void setSuperValue(String value) {
        super.setValue(value);
    }

    @Override
    public Node getUINode() {
        return textField;
    }

    @Override
    public void write(JmeExporter ex) throws IOException {
        super.write(ex);
        OutputCapsule capsule = ex.getCapsule(this);
        capsule.write(getValue(),"value","");
    }

    @Override
    public void read(JmeImporter im) throws IOException {
        super.read(im);
        InputCapsule capsule = im.getCapsule(this);
        setValue(capsule.readString("value",""));
    }

    public void setEnabled(boolean enable){
        textField.setEditable(enable);
    }
}
